<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>用户管理 </title>
	<#include '/commons/include/head.ftl'>
	<script src="${ctx}/static/assets/easyui/plugins/datagrid-detailview.js"></script>
</head>

<body>

	<div id="dg">
		<div id="toolbars" style="display:none;">
			<form id="searchForm">
				<div class="datagrid-toolbars">
					<div class="toolbar-row">
						<div class="group">
							<a onclick="reload();" href="javascript:;" class="bto"><i class="fa fa-refresh bto icon-green"></i>刷新</a>
							<div class="tools_separator"></div>
							<a onclick="form_dialog('add');" href="javascript:;" class="bto"><i class="fa fa-plus bto icon-green"></i>新增</a>
							<a onclick="form_dialog('update');" href="javascript:;" class="bto"><i class="fa fa-pencil bto icon-green1"></i>编辑</a>
							<a onclick="del();" href="javascript:;" class="bto"><i class="fa fa-trash bto icon-red"></i>删除</a>
							<a onclick="detail();" href="javascript:;" class="bto"><i class="fa fa-align-justify bto icon-blue"></i>详细</a>
							<div class="tools_separator"></div>
							<a onclick="reset_pwd_dialog();" href="javascript:;" class="bto"><i class="iconPng icon-key"></i>重置密码</a>
							<div class="tools_separator"></div>
							<a onclick="change_dept_dialog();" href="javascript:;" class="bto"><i class="iconPng icon-dept"></i>调换部门</a>
							<div class="tools_separator"></div>
							<a onclick="add_role_dialog();" href="javascript:;" class="bto"><i class="iconPng icon-role"></i>用户角色</a>
							<!--
							<a href="javascript:;" class="easyui-menubutton dropdown bto" data-options="menu: '#c'">
								<i class="fa fa-home bto icon-blue"></i>权限设置
							</a>
							<div id="c" class="easyui-menu" style="width: 140px;">
							    <div onclick="add_role_dialog();" data-options="iconCls: 'icon-hamburg-hire-me'">用户角色</div>
							    <div data-options="iconCls: 'icon-hamburg-lock'">用户权限</div>
							</div>
							-->
							<div class="tools_separator"></div>
							<a onclick="reset_pwd();" href="javascript:;" class="bto"><i class="iconPng icon-upload1"></i>导入</a>
							<a onclick="reset_pwd();" href="javascript:;" class="bto"><i class="iconPng icon-download1"></i>导出</a>
							
							<@shiro.hasRole name="gcssry">
								<a href="javascript:;" class="bto"><i class="fa fa-key bto icon-blue"></i>测试权限</a>
							</@shiro.hasRole>
						</div>
					</div>
				</div>
				<div class="datagrid-toolbars top-line">
					<div class="toolbar-row">
						<div class="group">
							<div class="form-inputs">
								<div class="sbox spe left-10">
									公司：<input id="company_search" name="company_id" style="width:160px;height:31px;" />
								</div>
								<div class="sbox">
									部门：<input id="dept_search" name="company_id" style="width:140px;height:31px;" />
								</div>
								<div class="tools_separator_min"></div>
								<div class="sbox">
									<input name="quickSearch" class="easyui-searchbox" data-options="prompt:'请输入关键字', height: 31" style="width:100px"></input>
								</div>
								<a onclick="searchBox();" href="javascript:;" class="">搜索</a>
								<a onclick="searchClear();" href="javascript:;" class="">重置</a>
							</div>
						</div>
					</div>
				</div>
			</form>
		</div>
	</div>
	
	
	<script>
	var $dg ;
	$(function(){
		$dg = $("#dg").datagrid({
            method: "GET", idField: "id", fit: true, 
            border: false, striped:true, pageSize: 30, pageList: [30,40,50,100,200,500,1000], 
            pagination: true, rownumbers: true, toolbar: "#toolbars", remoteSort: true, 
            sortName: 'sorting', sortOrder: 'asc', queryParams: {},
            frozenColumns: [[
                { field: "ck", checkbox: true },
                { field: "id", title: "ID", width: 80, sortable: true, hidden: true },
                { field: "account", title: "用户账号", width: 100, formatter:function(value,row){
                	return "<span style='line-height:30px;'>"+value+"</span>" ;
                }},
                { field: "truename", title: "真实姓名", width: 100 }
            ]],
            columns: [[
                { field: "spell", title: "姓名拼音", width: 150 },
                { field: "gender", title: "性别", width: 60, align: "center" },
                { field: "birthday", title: "出生日期", width: 90, align: "center" },
                { field: "mobile", title: "手机", width: 100, align: "center" },
                { field: "telephone", title: "电话", width: 100, align: "center" },
                { field: "email", title: "邮箱地址", width: 180 },
                { field: "locked", title: "是否锁定", width: 60, sortable: false, align: "center", formatter:function(value,row){
                	return (value==1?"正常":"<span style='color: red;'>禁用</span>") ;	
                }},
                { field: "company", title: "所在公司", width: 160, sortable: false, align: "center", formatter:function(value,row){
                	return row.company.fullName ;	
                }},
                { field: "dept", title: "所在部门", width: 120, sortable: false, align: "center", formatter:function(value,row){
                	return row.dept.fullName ;	
                }},
                { field: "logOnCount", title: "登录次数", width: 80, align: "center" },
                { field: "firstVisit", title: "第一次访问时间", width: 150, sortable: true, align: "center", formatter:function(value,row){
                	return (undefined != value?$.date.format(value, "yyyy-MM-dd HH:mm:ss"):"") ;	
                }},
                { field: "previousVisit", title: "上一次访问时间", width: 150, sortable: true, align: "center", formatter:function(value,row){
                	return (undefined != value?$.date.format(value, "yyyy-MM-dd HH:mm:ss"):"") ;	
                }},
                { field: "lastVisit", title: "最后访问时间", width: 150, sortable: true, align: "center", formatter:function(value,row){
                	return (undefined != value?$.date.format(value, "yyyy-MM-dd HH:mm:ss"):"") ;	
                }},
                { field: "createTime", title: "创建时间", width: 150, sortable: true, align: "center", formatter:function(value,row){
                	return (undefined != value?$.date.format(value, "yyyy-MM-dd HH:mm:ss"):"") ;	
                }},
                { field: "createUserName", title: "创建用户", width: 80, align: "center" },
                { field: "modifyTime", title: "修改时间", width: 150, sortable: true, align: "center", formatter:function(value,row){
                	return (undefined != value?$.date.format(value, "yyyy-MM-dd HH:mm:ss"):"") ;	
                }},
                { field: "modifyUserName", title: "修改用户", width: 80, align: "center" },
                { field: "remark", title: "备注", width: 200 }
            ]],
            view: detailview,
            detailFormatter:function(index,row){
                return '<div class="ddv" style="padding:5px 0;float:left;"><div id="roles"></div></div>';
            },
            onExpandRow: function(index,row){
                var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
                
                var rolesDiv = ddv.children("div#roles");
                rolesDiv.css({"border-right": "1px dashed #9F9F9F", "float": "left"});
                rolesDiv.empty();
            
                $.get("${ctx}/manager/base/user/getUserRoles/"+row.id, function(result){
                	var _dl = $("<dl></dl>");
                	_dl.css({"margin": "0", "padding": "0"});
                	
                	$("<dt style='color:#15428B; font-weight: bold;'>拥有角色</dt>").appendTo(_dl);
                	$.each(result, function(i, p){
                		$("<dd></dd>").html(p.fullName).appendTo(_dl).css({"margin": "0", "padding": "0 10px 0 20px"});;
                	});
                	_dl.appendTo(rolesDiv);
                	
                	$('#dg').datagrid('fixDetailRowHeight',index);
                });
                
                $('#dg').datagrid('fixDetailRowHeight',index);
            },
            onLoadSuccess: function(){
            	$.fn.datagrid.extensions.onLoadSuccess.apply(this, arguments);
            	$dg.datagrid('unselectAll');$dg.datagrid('clearSelections');
            },
            enableHeaderClickMenu: true, enableHeaderContextMenu: true, enableRowContextMenu: false
        });
        
       $company_search = $("#company_search").combotree({
			url : "${ctx}/manager/base/company/treegrid",
			editable: false, lines:true, method: "GET",
			onChange: function(newValue, oldValue) {
				if(undefined != oldValue && null != oldValue) {
					$.util.exec(function () {
						$dg.datagrid({url: "${ctx}/manager/base/user/datagrid", queryParams: {"company_id": newValue}});
					});
					$dept_search.combotree({
						url: "${ctx}/manager/base/dept/treegrid/"+newValue
					});
				}
            },
            onLoadSuccess: function(node,data) {
				$company_search.combotree("setValue", data[0].id);
			}
	    });
	    $dept_search = $("#dept_search").combotree({
			method: "GET", idFiled:'pid', editable: false, required:false, lines:true, autoShowPanel: false,
			onChange: function(newValue, oldValue) {
				if(undefined != oldValue && null != oldValue) {
					var company_id = $company_search.combotree("getValue") ;
					$dg.datagrid("load", {"company_id": company_id, "dept_id": newValue});
				}
            }
		});
	});
	
	
	function reload(){$dg.datagrid("reload") ;}
	function quickSearch(value,name){
		var o = {} ; o[name] = value ; $dg.datagrid("load",o);
	}
	function searchBox() {
		var search_form = $.util.serializeObject("#searchForm") ;
		var qs = search_form.quickSearch ;
		if(qs === "快速搜索..." || qs === "请输入关键字")search_form["quickSearch"]=null;
		$dg.datagrid("load",search_form);
	}
	function searchClear() {
		$("#searchForm input[name=quickSearch]").val("");
		$dept_search.combotree("setValue", null);
	}
	function getIds() {
		var rows = $dg.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			for ( var i = 0; i < rows.length; i++) {
				if(undefined != rows[i].id && ""!=rows[i].id) {
					ids.push(rows[i].id);
				}
			}
		}
		return ids ;
	}	
	
	function form_dialog(formType) {
		var form_url = "${ctx}/manager/base/user/form";
		if(formType === "update") {
			var ids = getIds() ;
			if(ids.length == 0) {alertify.warning("请选择一条记录！");return;}
			if(ids.length > 1) {alertify.warning("只能编辑一条记录！");return;}
			form_url = "${ctx}/manager/base/user/form?id="+ids[0];
		}
		
		var $d = $.easyui.showDialog({
			title: '<div class="icon-div"><i class="fa fa-th-large fa-120 pt-2 icon-blue"></i></div> 表单', 
			href: form_url, 
			iniframe: false, topMost: true, iconCls : '',
			width: 610, height: 300, maximizable: true,
            enableApplyButton: false, enableCloseButton: false, enableSaveButton: false,
            buttons : [ 
              { text : '<i class="fa fa-save fa-120 mt-6 icon-blue"></i> 提交', handler : function() { $.easyui.parent.submitForm($d, $dg) ; } },
              { text : '<i class="fa fa-remove fa-120 mt-6 icon-red"></i> 关闭', handler : function() { $d.dialog('destroy'); } } 
           	]
        });
	}
	
	function del() {
		var ids = getIds() ;
		if(ids.length == 0) {alertify.warning("请选择一条记录！");return;}
		$.messager.confirm("您确定要进行该操作？", function (c) { 
			if(c) {
				$.easyui.loading({ msg: "数据删除中，请稍等..."});
				deletedAjax("${ctx}/manager/base/user/deleteBatch", {"ids" : ids.join(',')}) ;
			}
		});
	}
	function deletedAjax(url, params) {
		$.ajax({
			url: url, data: params,
			type: "POST", dataType: "JSON",
			success: function(result) {
				if (result.status) {
					$dg.datagrid("unselectAll");$dg.datagrid("reload") ;
					alertify.success(result.message);
					$.easyui.loaded();
				} else {
					alertify.warning(result.message);
					$.easyui.loaded();
				}
			},
			complete: function(XMLHttpRequest, textStatus){},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				$.easyui.loaded();
				if(XMLHttpRequest.status == 403) {
					$.messager.alert(XMLHttpRequest.statusText, XMLHttpRequest.responseText, "warning");
				} else {
					$.messager.alert("错误", XMLHttpRequest.status + "：发生未知错误", "error");
				}
			}
		});
	}
	
	//添加角色到用户
	function add_role_dialog() {
		var rows = $dg.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			for ( var i = 0; i < rows.length; i++) {
				if(undefined != rows[i].id && ""!=rows[i].id) {
					ids.push(rows[i].id);
				}
			}
		}
		if(ids.length == 0) {alertify.warning("请选择一条记录！");return;}
		
		var $d = $.easyui.showDialog({
			href: "${ctx}/manager/base/user/addRoleToUser_dialog/"+rows[0].company_id, 
			title: "添加角色到用户", iniframe: false, topMost: true, width: 740, height: 547,
            enableApplyButton: false, enableCloseButton: false,  enableSaveButton: false,
            buttons : [ 
              { text : '<i class="fa fa-save fa-120 mt-6 icon-blue"></i> 提交', handler : function() { $.easyui.parent.submitForm($d, $dg) ; } },
              { text : '<i class="fa fa-remove fa-120 mt-6 icon-red"></i> 关闭', handler : function() { $d.dialog('destroy'); } } 
           	],
           	onLoad: function(){
           		parent.$("#user_ids").val(ids.join(","));
           	}
        });
	}
	
	//批量重置密码
	function reset_pwd_dialog() {
		var ids = getIds() ;
		if(ids.length == 0) {alertify.warning("请选择一条记录！");return;}
		
		var $d = $.easyui.showDialog({
			href: "${ctx}/manager/base/user/reset_pwd_dialog", 
			title: "批量重置用户密码", iniframe: false, topMost: true, width: 380, height: 180,
            enableApplyButton: false, enableCloseButton: false,  enableSaveButton: false,
            buttons : [ 
              { text : '<i class="fa fa-save fa-120 mt-6 icon-blue"></i> 提交', handler : function() { $.easyui.parent.submitForm($d, $dg) ; } },
              { text : '<i class="fa fa-remove fa-120 mt-6 icon-red"></i> 关闭', handler : function() { $d.dialog('destroy'); } } 
           	],
           	onLoad: function(){
           		parent.$("#user_ids").val(ids.join(","));
           	}
        });
	}
	
	//批量调换部门
	function change_dept_dialog() {
		var ids = getIds() ;
		if(ids.length == 0) {alertify.warning("请选择一条记录！");return;}
		
		var $d = $.easyui.showDialog({
			href: "${ctx}/manager/base/user/change_dept_dialog", 
			title: "批量调换部门", iniframe: false, topMost: true, width: 380, height: 180,
            enableApplyButton: false, enableCloseButton: false,  enableSaveButton: false,
            buttons : [ 
              { text : '<i class="fa fa-save fa-120 mt-6 icon-blue"></i> 提交', handler : function() { $.easyui.parent.submitForm($d, $dg) ; } },
              { text : '<i class="fa fa-remove fa-120 mt-6 icon-red"></i> 关闭', handler : function() { $d.dialog('destroy'); } } 
           	],
           	onLoad: function(){
           		parent.$("#user_ids").val(ids.join(","));
           	}
        });
	}
	</script>
</body>
</html>

